package com.wc.codeforces.离散化.Bouquet_Hard_Version;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/7/24 17:30
 * @description https://codeforces.com/contest/1995/problem/B2
 */
public class Main {
    /**
     * 思路: 见简单版
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    // a[i][0] 表示花瓣数, a[i][1]表示花瓣个数
    static long[][] a = new long[N][2];
    static int n;
    static long m;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            m = sc.nextLong();
            long res = 0;
            for (int i = 1; i <= n; i++) a[i][0] = sc.nextInt();
            for (int i = 1; i <= n; i++) a[i][1] = sc.nextInt();
            Arrays.sort(a, 1, n + 1, Comparator.comparing(o -> o[0]));
            for (int i = 1; i <= n; i++) {
                // 全买 p
                long tot = m, ans = 0;
                if (a[i][0] * a[i][1] <= tot) ans = a[i][0] * a[i][1];
                else ans = tot / a[i][0] * a[i][0];

                tot -= ans;
                // 看下相邻的是否可以买, 这里可能会收到上一题的影响
                if (a[i + 1][0] == a[i][0] + 1 && i + 1 <= n) {
                    // 买了多少个p
                    long number = ans / a[i][0];
                    long qnum = a[i + 1][1];
                    // 剩下的先买q
                    if (qnum * a[i + 1][0] <= tot) {
                        ans += qnum * a[i + 1][0];
                        qnum = 0;
                    } else {
                        long cy = tot / a[i + 1][0];
                        ans += cy * a[i + 1][0];
                        tot -= cy * a[i + 1][0];
                        qnum -= cy;
                    }
                    // 最多可以转换的数目
                    long mx = Math.min(number, qnum);
                    ans += Math.min(tot, mx);
                }
                res = Math.max(res, ans);
            }
            out.println(res);
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
